package org.dftproject.gedcom.ui.pages;

import java.io.File;
import java.util.Date;

import org.dftproject.genesis.core.collections.Collection;
import org.dftproject.genesis.core.collections.CollectionManager;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.dftproject.genesis.ui.pages.AbstractPagePart;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPageSite;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.PageUtils;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class AddGedcomPage extends AbstractPagePart {

	public static final String ID = AddGedcomPage.class.getName();
	
	private FigureCanvas canvas;

	public void init(IPageSite site, IPageInput input) throws PageInitException {
		setInput(input);
		setSite(site);
	}

	@Override
	public void createPartControl(Composite parent) {		
		createContents(parent);
	}
	
	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvas(parent);

		// Create the root figure

		Figure topContainer = new PageContainer();
		canvas.getViewport().setContentsTracksWidth(true);
		canvas.setContents(topContainer);
		
		topContainer.add(PageUtils.createH1Label("Open a GEDCOM file"));
		
		IFigure links = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		links.setLayoutManager(layout);
		links.setBorder(new MarginBorder(10, 10, 10, 0));
		topContainer.add(links);
		
		HyperlinkFigure addHyperlink = new HyperlinkFigure("Select the file...");
		addHyperlink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				
				FileDialog openDialog = new FileDialog(window.getShell(), SWT.OPEN);
				openDialog.setText("Open a GEDCOM File");
				openDialog.setFilterNames(new String[] { "GEDCOM Files (*.ged; *.gedcom)", "All Files (*.*)" });
				openDialog.setFilterExtensions(new String[] { "*.ged;*.gedcom", "*.*" });
				String gedcomFilename = openDialog.open();
				if (gedcomFilename != null) {
					Collection collection = new Collection("gedcom:" + gedcomFilename, getName(gedcomFilename), gedcomFilename, "org.dftproject.gedcom", gedcomFilename, new Date());
					collection = CollectionManager.getDefault().addCollection(collection);
					PageManager.getDefault().openPage(window.getActivePage(), new GedcomPageInput(gedcomFilename), GedcomPage.ID, true, false);
				}
			}
			
			protected String getName(String path) {
				File file = new File(path);
				return file.getName().replaceFirst("\\.[^.]*$", "");
			}
			
		});
		links.add(addHyperlink);
	}

	@Override
	public void dispose() {
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
